package com.job.servicejob.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.job.commonutils.R;
import com.job.servicejob.entity.JobApplication;
import com.job.servicejob.service.JobApplicationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author myt
 * @since 2023-03-21
 */
@RestController
@RequestMapping("/servicejob/job-application")
public class JobApplicationController {

    //把service注入

    @Autowired
    public JobApplicationService jobApplicationService;
    @GetMapping("findAll")
    public R findAllJob(){
        List<JobApplication> list = jobApplicationService.list(null);
        return R.ok().data("item",list);

    }
    //添加接口方法
    @PostMapping("addJobApply")
    public R addJob(@RequestBody JobApplication jobApplication){
        boolean save = jobApplicationService.save(jobApplication);
        if(save){
            return R.ok();
        }else {
            return R.error();
        }
    }
    //4条件查询带分页
    @PostMapping("pageJobCondition/{current}/{limit}")
    public R pageJobCondition(@PathVariable long current,@PathVariable long limit,
                              @RequestBody(required = false) JobApplication jobApplication){
        //创建page对象
        Page<JobApplication> pageJobApply = new Page<>(current,limit);
        //构建条件
        QueryWrapper<JobApplication> wrapper = new QueryWrapper<>();
        //多条件组合查询
        //动态sql
        //判断条件值是否为空，如果不空 拼接条件
        String job_name = jobApplication.getJobName();
        String name = jobApplication.getName();

        if(!StringUtils.isEmpty(job_name)){
            //构建条件
            wrapper.like("job_name",job_name);
        }
        if(!StringUtils.isEmpty(name)){
            //构建条件
            wrapper.like("name",name);
        }

        //调用方法实现分页查询
        jobApplicationService.page(pageJobApply,wrapper);
        long total = pageJobApply.getTotal();//总记录数
        List<JobApplication> records = pageJobApply.getRecords();//数据list集合
        return R.ok().data("total",total).data("rows",records);
    }

    //修改操作
    //1.根据id查询岗位
    @GetMapping("getJobApply/{id}")
    public R getJob(@PathVariable String id){
        JobApplication jobApplication = jobApplicationService.getById(id);
        return R.ok().data("job",jobApplication);

    }
    //2.修改
    @PostMapping("updateJobApply")
    public R updateJob (@RequestBody JobApplication jobApplication){
        boolean flag = jobApplicationService.updateById(jobApplication);
        if(flag){
            return R.ok();
        }else {
            return R.error();
        }
    }

    //逻辑删除job
    @DeleteMapping("{id}")
    //@PathVariable String id得到路径中的id值
    public R removeJob(@PathVariable Integer id){
        boolean flag = jobApplicationService.removeById(id);
        if (flag){
            return R.ok();
        }else {
            return R.error();
        }
    }

}

